package org.szgb.console.ht.meter.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.line.entity.SysLineLocation;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtMeterReadingTask extends BeanEntity<HtMeterReadingTask> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLineLocation location;
    private String name;

    public HtMeterReadingTask() {
    }

    public HtMeterReadingTask(String id) {
        this.setId(id);
    }


    public HtMeterReadingTask(HtMeterReadingTask.HtMeterReadingTaskFindDto htMeterReadingTaskFindDto){
        this.setId(htMeterReadingTaskFindDto.getId());
        this.setLocation(new SysLineLocation(htMeterReadingTaskFindDto.getLocationId()));
        this.setName(htMeterReadingTaskFindDto.getName());
        this.setPageDto(htMeterReadingTaskFindDto.getPageDto());
    }

    public HtMeterReadingTask(HtMeterReadingTask.HtMeterReadingTaskTodayNoDealFindDto htMeterReadingTaskTodayNoDealFindDto){
        this.setLocation(new SysLineLocation(htMeterReadingTaskTodayNoDealFindDto.getLocationId()));
    }

    public HtMeterReadingTask(HtMeterReadingTask.HtMeterReadingTaskSaveDto htMeterReadingTaskSaveDto){
        this.setId(htMeterReadingTaskSaveDto.id);
        this.setLocation(new SysLineLocation(htMeterReadingTaskSaveDto.locationId));
        this.setName(htMeterReadingTaskSaveDto.name);
    }

    public HtMeterReadingTask.HtMeterReadingTaskView toHtMeterReadingTaskView() {
        return new HtMeterReadingTask.HtMeterReadingTaskView(this);
    }

    @Data
    @Schema(title = "表计记录任务信息", description = "表计记录任务信息")
    public static class HtMeterReadingTaskView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "表计名称")
        private String name;
        @Schema(description = "站点id")
        private String locationId;
        @Schema(description = "站点名称")
        private String locationName;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtMeterReadingTaskView(HtMeterReadingTask htMeterReadingTask) {
            this.id = htMeterReadingTask.getId();
            this.name = htMeterReadingTask.getName();
            this.locationId = htMeterReadingTask.getLocation().getId();
            this.locationName = htMeterReadingTask.getLocation().getName();
            this.createdAt = htMeterReadingTask.getCreatedAt();
            this.updatedAt = htMeterReadingTask.getUpdatedAt();
            if (htMeterReadingTask.getCreatedBy() != null) {
                this.createdById = htMeterReadingTask.getCreatedBy().getId();
                this.createdByName = htMeterReadingTask.getCreatedBy().getName();
            }
            if (htMeterReadingTask.getUpdatedBy() != null) {
                this.updatedById = htMeterReadingTask.getUpdatedBy().getId();
                this.updatedByName = htMeterReadingTask.getUpdatedBy().getName();
            }
        }

        public static List<HtMeterReadingTask.HtMeterReadingTaskView> getViewListFromList(List<HtMeterReadingTask> htMeterReadingTaskList) {
            return htMeterReadingTaskList.stream().map(HtMeterReadingTask.HtMeterReadingTaskView::new).toList();
        }
    }


    @Data
    @Schema(title = "表计记录任务查询表单", description = "表计记录任务查询参数")
    public static class HtMeterReadingTaskFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "表计名称")
        private String name;
        @Schema(description = "站点id")
        private String locationId;
        @Schema(description = "分页信息")
        private PageDto pageDto;

    public HtMeterReadingTask toHtMeterReadingTask() {
            return new HtMeterReadingTask(this);
        }
    }

    @Data
    @Schema(title = "当天需要填写任务查询表单", description = "当天需要填写任务查询参数")
    public static class HtMeterReadingTaskTodayNoDealFindDto {
        @Schema(description = "站点id")
        private String locationId;

        public HtMeterReadingTask toHtMeterReadingTask() {
            return new HtMeterReadingTask(this);
        }
    }

    @Data
    @Schema(title = "表计记录任务保存表单", description = "表计记录任务保存参数")
    public static class HtMeterReadingTaskSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "表计名称")
        private String name;
        @Schema(description = "站点id")
        private String locationId;

    public HtMeterReadingTask toHtMeterReadingTask() {
            return new HtMeterReadingTask(this);
        }
    }
}
